package com.spring_book.spring_book.service;

import com.spring_book.spring_book.enums.BookStatusEnum;
import com.spring_book.spring_book.mapper.BookMapper;
import com.spring_book.spring_book.model.BookInfo;
import com.spring_book.spring_book.model.PageRequest;
import com.spring_book.spring_book.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {


    @Autowired
    private BookMapper bookMapper;


    public Integer insertBook(BookInfo bookInfo) {
         return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        //1.总记录数
        Integer count = bookMapper.count();
        //2.当前页的记录
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        for(BookInfo bookInfo : bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
//            if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else {
//                bookInfo.setStatusCN("不可借阅");
//            }
        }
        return new PageResponse<BookInfo>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
